
from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal
from threading import Timer

import record as r
import json


class JsHandler(QObject):
  def __init__(self, frame):
    super(JsHandler, self).__init__()
    self.frame = frame
    r.startRecord(-1, self.logMsg, self);
    t = Timer(10.0, self.stopRecord)
    t.start()

  def loaded(self, ok):
    #initial actions with 
    self.frame.addToJavaScriptWindowObject("js2Qt", self);
    devs = r.deviceList();
    self.frame.evaluateJavaScript('showDevices('+json.dumps(devs)+');')

  @pyqtSlot(int)
  def startRecord(self, dev=-1):
    r.startRecord(dev, self.logMsg);
  
  @pyqtSlot()
  def stopRecord(self):
    r.stopRecord(self.logMsg);

  def logMsg(self, data):
    self.frame.evaluateJavaScript('log('+json.dumps(data)+');')
    

